package com.atguigu.flink.chapter10;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.runtime.state.storage.JobManagerCheckpointStorage;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

/**
 * @Author lzc
 * @Date 2023/6/27 09:23
 */
public class KafkaConnectorDemo {
    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
//        conf.setInteger("rest.port", 2000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
        env.enableCheckpointing(3000);
        env.getCheckpointConfig().setCheckpointStorage(new JobManagerCheckpointStorage());
        
        StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);
        // 通过 ddl 的方式创建连接器表
        tEnv.executeSql("create table sensor(" +
                            " id string, " +
                            " ts bigint, " +
                            " vc int " +
                            ")with(" +
                            "  'connector' = 'kafka', " +
                            "  'topic' = 's1', " +
                            "  'properties.bootstrap.servers' = 'hadoop162:9092', " +
                            "  'properties.group.id' = 'atguigu', " +
                            "  'scan.startup.mode' = 'latest-offset', " +
                            "  'format' = 'csv' " +
                            ")");
    
        Table result = tEnv.sqlQuery("select * from sensor where vc > 10");
    
        tEnv.executeSql("create table a(" +
                            " id string, " +
                            " ts bigint, " +
                            " vc int " +
                            ")with(" +
                            "  'connector' = 'print' " +
                            ")");
    
        tEnv.executeSql("create table b(" +
                            " id string, " +
                            " ts bigint, " +
                            " vc int " +
                            ")with(" +
                            "  'connector' = 'print' " +
                            ")");
    
        result.executeInsert("a");
        result.executeInsert("b");
       
        tEnv.executeSql("create table s2(" +
                            " id string, " +
                            " ts bigint, " +
                            " vc int " +
                            ")with(" +
                            "  'connector' = 'kafka', " +
                            "  'topic' = 's2', " +
                            "  'properties.bootstrap.servers' = 'hadoop162:9092', " +
                            "  'format' = 'csv', " +
                            "  'sink.semantic' = 'exactly-once', " +
                            "  'sink.transactional-id-prefix' = 'atguigu', " +
                            "  'properties.transaction.timeout.ms' = '90000' " +
                            ")");
    
        result.executeInsert("s2");
    
    
    }
}
